1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.base.Function;
21 import com.google.common.base.Predicate;
22 import com.google.common.collect.Maps.EntryTransformer;
23
24 import java.lang.reflect.Array;
25 import java.util.Collections;
26 import java.util.Map;
27 import java.util.NavigableMap;
28 import java.util.NavigableSet;
29 import java.util.Set;
30 import java.util.SortedMap;
31 import java.util.SortedSet;
32
33
34
35
36
37
38 @GwtCompatible(emulated = true)
39 final class Platform {
40
41
42
43
44
45
46
47 static <T> T[] newArray(T[] reference, int length) {
48 Class<?> type = reference.getClass().getComponentType();
49
50
51
52 @SuppressWarnings("unchecked")
53 T[] result = (T[]) Array.newInstance(type, length);
54 return result;
55 }
56
57 static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
58 return Collections.newSetFromMap(map);
59 }
60
61
62
63
64
65
66
67 static MapMaker tryWeakKeys(MapMaker mapMaker) {
68 return mapMaker.weakKeys();
69 }
70
71 static <K, V1, V2> SortedMap<K, V2> mapsTransformEntriesSortedMap(
72 SortedMap<K, V1> fromMap,
73 EntryTransformer<? super K, ? super V1, V2> transformer) {
74 return (fromMap instanceof NavigableMap)
75 ? Maps.transformEntries((NavigableMap<K, V1>) fromMap, transformer)
76 : Maps.transformEntriesIgnoreNavigable(fromMap, transformer);
77 }
78
79 static <K, V> SortedMap<K, V> mapsAsMapSortedSet(SortedSet<K> set,
80 Function<? super K, V> function) {
81 return (set instanceof NavigableSet)
82 ? Maps.asMap((NavigableSet<K>) set, function)
83 : Maps.asMapSortedIgnoreNavigable(set, function);
84 }
85
86 static <E> SortedSet<E> setsFilterSortedSet(SortedSet<E> set,
87 Predicate<? super E> predicate) {
88 return (set instanceof NavigableSet)
89 ? Sets.filter((NavigableSet<E>) set, predicate)
90 : Sets.filterSortedIgnoreNavigable(set, predicate);
91 }
92
93 static <K, V> SortedMap<K, V> mapsFilterSortedMap(SortedMap<K, V> map,
94 Predicate<? super Map.Entry<K, V>> predicate) {
95 return (map instanceof NavigableMap)
96 ? Maps.filterEntries((NavigableMap<K, V>) map, predicate)
97 : Maps.filterSortedIgnoreNavigable(map, predicate);
98 }
99
100 private Platform() {}
101 }